Two Methods for PHP to Realize Composite Pictures Based on imagick Extension [Attached with imagick Extension Download]

  • 2021-08-21 19:50:33
  • OfStack

In this paper, two methods of PHP based on imagick extension to synthesize pictures are described by examples. Share it for your reference, as follows:

Method 1: compositeimages


/**
* function:  Composite picture 
* @param string   $output_url  Picture save path 
* @param string   $img_type   Picture save type 
* @param integral  $line_num   Number of pictures per line 
* @param array    $logo_info  Information of each picture to be synthesized (all size systems are required 1 ) 
* @param array    $img_list  Absolute path of picture to be synthesized 
*
* @return void
*/
public function generate($output_url, $img_type, $line_num, $logo_info, $img_list=array()) {
    // Calculate how many lines a picture has 
    $lines = ceil(count($img_list)/$line_num);
    $bg_width = ($logo_info['width'] + $logo_info['line_width']) * $line_num;
    $bg_height = ($logo_info['height'] + $logo_info['line_height']) * $lines;
    // Build canvas 
    $canvas = new Imagick();
    $canvas->newimage($bg_width, $bg_height, 'white');
    $canvas->setimageformat($img_type);
    $i = $j = 0;
    foreach ($img_list as $item) {
      $im = new Imagick($item);
      $x = $logo_info['line_width']*2 + $i * $logo_info['width'];
      $y = $logo_info['line_height']*2 + $j * $logo_info['height'];
      // $canvas->compositeimage($im -> getimage(), Imagick::COMPOSITE_OVER, $x, $y);
      $canvas -> compositeimage($im, $im->getImageCompose(), $x, $y);
      if (($i + 1) % $line_num === 0) {
        $i = 0;
        $j++;
      } else {
        $i++;
      }
      // unset($im);
      $im -> destroy();
    }
    $canvas->writeimage($output_url);
    // Destroy object 
    $canvas -> destroy();
}

Method 2: combineimages


/**
* function:  Composite picture 
* @param string   $output_url  Picture save path 
* @param string   $img_type   Picture save type 
* @param integral  $line_num   Number of pictures per line 
* @param array    $logo_info  Information of each picture to be synthesized (all size systems are required 1 ) 
* @param array    $img_list  Absolute path of picture to be synthesized 
*
* @return void
*/
public function generate($output_url, $img_type, $line_num, $logo_info, $img_list=array()) {
    // Calculate how many lines a picture has 
    $lines = ceil(count($img_list)/$line_num);
    $bg_width = ($logo_info['width'] + $logo_info['line_width']) * $line_num;
    $bg_height = ($logo_info['height'] + $logo_info['line_height']) * $lines;
    // Build canvas 
    $canvas = null;
    $canvas = new Imagick();
    $canvas -> newimage($bg_width, $bg_height, 'white');
    $i = $j = 0;
    foreach ($img_list as $item) {
      $im = null;
      $im = new Imagick($item);
      // $canvas -> readImage($item);
      $canvas -> addimage($im);
      $im -> clear();
      $im -> destroy();
    }
    // $canvas -> flattenImages();
    $canvas -> combineImages( Imagick::CHANNEL_ALL );
    $canvas -> writeimage($output_url);
    // Destroy object 
    $canvas -> clear();
    $canvas -> destroy();
}

Personally, I think Method 2 is more efficient

Attachment: imagick component downloads for php 5.2. 17 and php 5.3. 18 and php 5.4. 8 versions below windows

1. I installed PHP 5.2. 17 successfully. I used the off-satellite PHP 5.2. 17 direct installation program. Download imagick version as follows:

Click here to download this site.

You don't need to install anything to include imagemagick, just copy php_imagick_st-Q16. dll to the c:\ php\ ext directory
Then add extension=php_imagick_st-Q16. dll to the php. ini extension
ps: There are also such things as php_imagick_st-Q8.dll (8 colors), php_imagick_dyn-Q16. dll (need to install imagemagick), so it is completely unnecessary to care about them

PHP5.2. 17 Download address of this site:

32 bits: https://www.ofstack.com/softs/26087. html

64-bit: https://www.ofstack.com/softs/479475. html

2. PHP 5.3. 18 imagemagick must be installed. There is no single file dll available

1) Official download address:

http://www.imagemagick.org/download/binaries/ImageMagick-6.8.0-4-Q16-windows-dll.exe

2) Download php_imagick according to your php installation mode

Thread safety:

Click here to download this site.

Copy to the c:\ php\ ext directory, adding extensions such as: extension=php_imagick_nts. dll

3. PHP 5.4. 8 imagemagick must be installed. There is no single file dll available

1) Official download address:

http://www.imagemagick.org/download/binaries/ImageMagick-6.8.0-4-Q16-windows-dll.exe

2) Download php_imagick according to your PHP installation mode

Wireless process safety:

Click here to download this site.

Copy to the c:\ php\ ext directory, adding extensions such as: extension=php_imagick. dll

I have tested all the above installations. Restart the iis or Apache server, and then check phpinfo to see if there is imagick. If not, please check the permissions or do it again.

For more readers interested in PHP related content, please check the topics on this site: "Summary of PHP Graphics and Pictures Operation Skills", "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage" and "Summary of PHP Mathematical Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: